#include "plurkapi.h"
#include <QScriptEngine>

#if 0
PlurkAPI::PlurkAPI(QObject *parent) :
    QObject(parent)
{
  connect(&oauth, SIGNAL(performAuth(const QString &)),
          this, SLOT(performAuth(const QString &)));
  connect(&oauth, SIGNAL(finishAccess()),
          this, SLOT(finishAccess()));
  connect(&oauth, SIGNAL(appRequestFinish(const QUrl &, const QString &)),
          this, SLOT(appRequestFinish(const QUrl &, const QString &)));
}
#endif

PlurkAPI::PlurkAPI() : QObject(0)
{
  plurksReply = NULL;
  responsesReply = NULL;

  connect(&oauth, SIGNAL(performAuth(const QString &)),
          this, SLOT(performAuth(const QString &)));
  connect(&oauth, SIGNAL(finishAccess()),
          this, SLOT(finishAccess()));
  connect(&oauth, SIGNAL(appRequestFinish(const QUrl &, const QString &)),
          this, SLOT(appRequestFinish(const QUrl &, const QString &)));
  connect(&oauth, SIGNAL(appRequestError(const QUrl &, const QString &)),
          this, SLOT(appRequestError(const QUrl &, const QString &)));
}

void PlurkAPI::setConsumerKey(QString consumerKey)
{
  oauth.setConsumerKey(consumerKey);
  _consumerKey = consumerKey;
}

void PlurkAPI::setConsumerSecret(QString consumerSecret)
{
  oauth.setConsumerSecret(consumerSecret);
  _consumerSecret = consumerSecret;
}

void PlurkAPI::setAppKey(QString appKey)
{
  oauth.setAppKey(appKey);
  _appKey = appKey;
}

void PlurkAPI::setAppSecret(QString appSecret)
{
  oauth.setAppSecret(appSecret);
  _appSecret = appSecret;
}

QString PlurkAPI::consumerKey()
{
  return _consumerKey;
}

QString PlurkAPI::consumerSecret()
{
  return _consumerSecret;
}

QString PlurkAPI::appKey()
{
  return _appKey;
}

QString PlurkAPI::appSecret()
{
  return _appSecret;
}

void PlurkAPI::initRequestToken()
{
  oauth.requestToken("POST", PLURK_REQUEST_TOKEN);
}

void PlurkAPI::startAccessToken(QString verifier)
{
  _tokenVerifier = verifier;
  oauth.setTokenVerifier(verifier);
  oauth.requestAccessToken("POST", PLURK_ACCESS_TOKEN);
}

void PlurkAPI::performAuth(const QString &token)
{
  emit openAuthUrl(QUrl(PLURK_AUTHORIZE + token));
}

void PlurkAPI::finishAccess()
{
  _appKey = oauth.appKey();
  _appSecret = oauth.appSecret();
  emit finishOAuth();
}

void PlurkAPI::GetUnreadCount()
{
  oauth.requestApplication("GET", PLURK_UNREAD_COUNT);
}

void PlurkAPI::AbortPlurks()
{
  if (plurksReply == NULL) return;
  if (!plurksReply->isFinished()) plurksReply->abort();
  plurksReply->deleteLater();
  plurksReply = NULL;
}

void PlurkAPI::GetPlurks()
{
  QMap<QString, QString> map;
  map["limit"] = "20";
  plurksReply = oauth.requestApplication("POST", PLURK_GET_PLURKS, map);
}

void PlurkAPI::AbortResponses()
{
  if (responsesReply == NULL) return;
  if (!responsesReply->isFinished()) responsesReply->abort();
  responsesReply->deleteLater();
  responsesReply = NULL;
}

void PlurkAPI::GetResponses(int id)
{
  QMap<QString, QString> map;
  map["plurk_id"] = QString::number(id);
  responsesReply = oauth.requestApplication("POST", PLURK_GET_RESPONSE, map);
}

void PlurkAPI::PlurkAdd(const QString &content)
{
  QMap<QString, QString> map;
  map["content"]  = content;
  map["qualifier"]  = ":";
  oauth.requestApplication("POST", PLURK_ADD_PLURK, map);
}

void PlurkAPI::ResponseAdd(int id, const QString &content)
{
  QMap<QString, QString> map;
  map["plurk_id"] = QString::number(id);
  map["content"]  = content;
  map["qualifier"]  = ":";
  oauth.requestApplication("POST", PLURK_ADD_RESPONSE, map);
}

void PlurkAPI::appRequestFinish(const QUrl &url, const QString &data)
{
  QScriptEngine engine;

  if (url.toString() == PLURK_UNREAD_COUNT) {
    emit unreadCount(engine.evaluate('(' + data + ')').toVariant());
  } else if (url.toString() == PLURK_GET_PLURKS) {
    emit plurksReceived(engine.evaluate('(' + data + ')').toVariant());
    plurksReply->deleteLater();
    plurksReply = NULL;
  } else if (url.toString() == PLURK_GET_RESPONSE) {
    emit responsesReceived(engine.evaluate('(' + data + ')').toVariant());
    responsesReply->deleteLater();
    responsesReply = NULL;
  } else if (url.toString() == PLURK_ADD_PLURK) {
    emit plurkAdded();
  } else if (url.toString() == PLURK_ADD_RESPONSE) {
    emit responseAdded();
  }
}

void PlurkAPI::appRequestError(const QUrl &url, const QString &data)
{
  Q_UNUSED(url);
  Q_UNUSED(data);

  qDebug() << " Request Error " << url << data;

  emit plurkRequestError();
}
